

package com.songbl.bless.app.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songbl.bless.app.dao.ContactsGroupDao;
import com.songbl.bless.app.entity.ContactsEntity;
import com.songbl.bless.app.entity.ContactsGroupEntity;
import com.songbl.bless.app.entity.UserAndGroupEntity;
import com.songbl.bless.app.form.ContractsGroupForm;
import com.songbl.bless.app.service.ContactsGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *  MVC:
 *   C:控制层调用 Service（业务层） ，service调用dao（持久层-由于mybatis不再写具体实现，直接使用）。
 *
 */
@Service("contactsGroupService")
public class ContactsGroupServiceImpl extends ServiceImpl<ContactsGroupDao, ContactsGroupEntity> implements ContactsGroupService {

	@Override
	public List<ContactsGroupEntity> getContactsGroup(ContractsGroupForm form) {
		QueryWrapper queryWrapper  = new QueryWrapper();
		queryWrapper.eq("user_id",form.getUserId());
		return baseMapper.selectList(queryWrapper);
	}

	@Override
	public List<UserAndGroupEntity> queryUserAndListById(long uId) {
		return baseMapper.queryUserAndListById(uId);
	}

	// baseMapper.自定义方法，其实调用的是dao中定义的方法，然后去找对应的xml
	@Override
	public List<UserAndGroupEntity> queryUserAndGroupAndContactsById(long uId) {
		return baseMapper.queryUserAndGroupAndContactsById(uId);
	}

	@Override
	public Boolean addContractGroup(ContactsGroupEntity addContractGroupForm) {

		int insert = baseMapper.insert(addContractGroupForm);

		return insert==1;
	}

	/**
	 * 只需要一个组就可以删除
	 *{
	 * 	"groupId": 11,
	 * 	"groupName": "德华的朋友",
	 * 	"userId": 7
	 * }
	 *
	 * {
	 *     "msg": "success",
	 *     "result": true,
	 *     "code": 0
	 * }
	 * @param contactsGroupEntity
	 * @return
	 */
	@Override
	public Boolean deleteContractGroup(ContactsGroupEntity contactsGroupEntity) {
		QueryWrapper queryWrapper  = new QueryWrapper();
		queryWrapper.eq("group_id",contactsGroupEntity.getGroupId());
		int delete = baseMapper.delete(queryWrapper);
		System.out.println("删除组"+"" +delete);
		return delete==1;

	}

	@Override
	public Boolean updateContractGroup(ContactsGroupEntity ContractGroupForm) {
		QueryWrapper queryWrapper  = new QueryWrapper();
		queryWrapper.eq("group_id",ContractGroupForm.getGroupId());
		int update = baseMapper.update(ContractGroupForm,queryWrapper);
		System.out.println("修改组"+"" +update);
		return update==1;
	}


}
